﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataModel.Entity;
using SmartMD.DataAccess.Service;
using System.Data;
using System.Collections.ObjectModel;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class DataDictDaoImpl : BaseGenericDaoImpl
    {
        public bool saveDictionaryType(List<DictionaryType> dictTypes)
        {
            bool isSaved = true;
            DictionaryType dictType;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dictTypes.Count; i++)
            {
                dictType = dictTypes[i];
                bool dictTypeResult = saveOrUpdateObject(dictType);
               isSaved = isSaved & dictTypeResult;
            }
            return isSaved;
        }

        public bool saveOrUpdateObject(DictionaryType dictType)
        {
            string deleteSql = "delete from DUO_DIC_TYPE where DICTYPEID = '" + dictType.Id + "'";
            bool isDeleted = this.dbConn.Delete(deleteSql);
          
            List<DictionaryItem> dictItemList = new List<DictionaryItem>();
            DictionaryItem dictItem;
            for (int j = 0; j < dictType.Items.Count; j++)
            {
                dictItem = dictType.Items[j];
                dictItem.DictTypeId = dictType.Id;
                dictItemList.Add(dictItem);
            }
            bool dictItemResult = DataServiceManager.DictItemService.saveDictItems(dictItemList);

            StringBuilder sb = new StringBuilder();
            sb.Append(" insert into");
            sb.Append(" DUO_DIC_TYPE (DICTYPEID, ENAME, CNAME, DESCRIPTION, GROUPINFO)");
            sb.Append(" values(");
            sb.Append(" '" + dictType.Id + "',");
            sb.Append(" '" + dictType.Code + "',");
            sb.Append(" '" + dictType.Name + "',");
            sb.Append(" '" + dictType.Description + "',");
            sb.Append(" '" + dictType.Groupinfo + "'");
            sb.Append(")");
            bool success = this.dbConn.Insert(sb.ToString());

            return success & dictItemResult;
        }

        public List<DictionaryType> loadAllDictianryInfo()
        {
            List<DictionaryType> dictTypeList = new List<DictionaryType>();
            DictionaryType dictType;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_DIC_TYPE");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    dictType = new DictionaryType();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        dictType.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    //查询字典项表
                    List<DictionaryItem> dictItemTmpList = DataServiceManager.DictItemService.loadDictItemById(dictType.Id);
                    ObservableCollection<DictionaryItem> dictItemList = new ObservableCollection<DictionaryItem>();
                    for (int i = 0; i < dictItemTmpList.Count; i++)
                    {
                        dictItemList.Add(dictItemTmpList[i]);
                    }
                    dictType.Items = dictItemList;
                    dictTypeList.Add(dictType);
                }
                return dictTypeList;
            }
            else
                return null;
        }

        public DictionaryType loadDictianryByName(string name)
        {
         
            DictionaryType dictType = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_DIC_TYPE");
            sb.Append(" where CNAME ='" + name + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    dictType = new DictionaryType();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        dictType.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    //查询字典项表
                    List<DictionaryItem> dictItemTmpList = DataServiceManager.DictItemService.loadDictItemById(dictType.Id);
                    ObservableCollection<DictionaryItem> dictItemList = new ObservableCollection<DictionaryItem>();
                    for (int i = 0; i < dictItemTmpList.Count; i++)
                    {
                        dictItemList.Add(dictItemTmpList[i]);
                    }
                    dictType.Items = dictItemList;
                   
                }
                return dictType;
            }
            else
                return null;
        }
    }
}
